package com.gxa.autoparts.portal.service.impl;

import com.gxa.autoparts.core.common.ExceptionCode;
import com.gxa.autoparts.core.exception.BusinessException;
import com.gxa.autoparts.core.pojo.AttributeKey;
import com.gxa.autoparts.core.pojo.AttributeValue;
import com.gxa.autoparts.core.pojo.GoodsSku;
import com.gxa.autoparts.core.util.JsonUtil;
import com.gxa.autoparts.portal.mapper.AttributeKeyMapper;
import com.gxa.autoparts.portal.mapper.AttributeValueMapper;
import com.gxa.autoparts.portal.mapper.GoodsSkuMapper;
import com.gxa.autoparts.portal.service.AttributeKeyService;
import org.omg.CORBA.OBJECT_NOT_EXIST;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author ：looker
 * @date ：Created in 2022/1/11 11:27
 */
@Service
public class AttributeKeyServiceImpl implements AttributeKeyService {

    @Autowired
    AttributeKeyMapper attributeKeyMapper;

    @Autowired
    AttributeValueMapper attributeValueMapper;

    @Autowired
    GoodsSkuMapper goodsSkuMapper;


    @Override
    public List<Map<String, Object>> getAttribute(List<GoodsSku> skus) throws BusinessException {
        Map<String,List<AttributeValue>> attrValue = new HashMap<>();
        int k = 0;
        for (GoodsSku goodsSku : skus) {
            String productSpecs = goodsSku.getProductSpecs();
            Map<String,Object> map = JsonUtil.readToObject(productSpecs, Map.class);
            Set<Map.Entry<String, Object>> entries = map.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                if(k==0){
                    ArrayList<AttributeValue> strings = new ArrayList<>();
                    attrValue.put(entry.getKey(),strings);

                    AttributeValue attributeValueByName = this.getAttributeValueByName(entry.getValue().toString());

                    strings.add(attributeValueByName);
                }else {
                    AttributeValue attributeValueByName = this.getAttributeValueByName(entry.getValue().toString());
                    List<AttributeValue> attributeValues = attrValue.get(entry.getKey());
                    if (!attributeValues.contains(attributeValueByName)) {
                        attributeValues.add(attributeValueByName);
                    }
                }
            }
            k++;
        }
        ArrayList<Map<String,Object>> objects = new ArrayList<>();
        Set<Map.Entry<String, List<AttributeValue>>> entries = attrValue.entrySet();
        for (Map.Entry<String, List<AttributeValue>> entry : entries) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("stand",entry.getKey());
            map.put("value",entry.getValue());
            objects.add(map);
        }
        return objects;
    }

    @Override
    public List<AttributeValue> getSonAttribute(Long keyId) {
        Example example = new Example(AttributeValue.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("attributeKeyId",keyId);
        List<AttributeValue> attributeValues = attributeValueMapper.selectByExample(example);
        return attributeValues;
    }

    @Override
    public AttributeValue getAttributeValueByName(String name){
        Example example = new Example(AttributeValue.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("attributeValue",name);
        List<AttributeValue> attributeValues = attributeValueMapper.selectByExample(example);
        if (attributeValues.size()>0) {
            return attributeValues.get(0);
        }
        return null;
    }
}
